﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class NewsforADMIN : System.Web.UI.Page
{
    private string strconn;
    SqlCommand comm;
    SqlConnection conn;
    DataSet ds;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        strconn = "Server = localhost;database=photofpt;trusted_connection=true";
        conn = new SqlConnection(strconn);
        ds = new DataSet();
        if (!IsPostBack)
            bindproducttogridview();
    }

    protected void bindproducttogridview()
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            comm = new SqlCommand("deNews", conn);
            comm.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comm);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.SelectedRowStyle.BackColor = System.Drawing.Color.Yellow;
        txtID.Text = GridView1.SelectedRow.Cells[0].Text;
        txtTitle.Text = GridView1.SelectedRow.Cells[1].Text;
        txtNews.Text = GridView1.SelectedRow.Cells[2].Text;
        //Image1.ImageUrl = GridView1.SelectedRow.Cells[3].Text;
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            String sql = "SELECT Photo FROM news WHERE ID =" + txtID.Text;
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read()){
                Image1.Visible=true;
                Image1.ImageUrl= rd[0].ToString();
            }
            
        }
        finally {
            conn.Close();
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {   
        try
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            else
                conn.Open();
            comm = new SqlCommand("deAdd", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add("@Title", SqlDbType.VarChar).Value = txtTitle.Text;
            comm.Parameters.Add("@News", SqlDbType.VarChar).Value = txtNews.Text;
            string img = FileUpload1.FileName.ToString();
            comm.Parameters.Add("@Photo", SqlDbType.VarChar).Value = "~\\images\\" + img;
            comm.ExecuteNonQuery();
            FileUpload1.SaveAs(Server.MapPath("~\\images\\" + img));

            bindproducttogridview();
            txtTitle.Text = "";
            txtNews.Text = "";
            Image1.ImageUrl = "";

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
                conn.Open();

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            comm = new SqlCommand("deDelete", conn);
            comm.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            comm.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comm);
            comm.ExecuteNonQuery();
            bindproducttogridview();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //ds = new DataSet();
        if (conn.State == ConnectionState.Open)
            conn.Close();
        else
            conn.Open();

        comm = new SqlCommand("deUpdate", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add("@ID", SqlDbType.VarChar).Value = txtID.Text.Trim();
        comm.Parameters.Add("@Title", SqlDbType.VarChar).Value = txtTitle.Text.Trim();
        comm.Parameters.Add("@News", SqlDbType.VarChar).Value = txtNews.Text.Trim();
        string img = FileUpload1.FileName.ToString();
        comm.Parameters.Add("@Photo", SqlDbType.VarChar).Value = "~\\images\\" + img;
        comm.ExecuteNonQuery();
        FileUpload1.SaveAs(Server.MapPath("~\\images\\" + img));
        GridView1.EditIndex = -1;
        bindproducttogridview();
        Image1.Visible = true;
        txtTitle.Text = "";
        txtNews.Text = "";
        Image1.ImageUrl = "";
        conn.Close();
      
        
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            //GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            // id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            comm = new SqlCommand("deDelete", conn);
            comm.Parameters.Add("@ID", SqlDbType.VarChar).Value = txtID.Text.Trim();
            comm.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comm);
            comm.ExecuteNonQuery();
            bindproducttogridview();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

}